using System;
using System.Collections.Generic;
using System.BusinessObjects.Data;
using Blazing.BusinessObjects.Queries;
using System.Xml.Serialization;
using Blazing.BusinessObjects.Tagging;
using Iesi.Collections.Generic;
using NHibernate;

namespace Blazing.BusinessObjects.Cms
{
    [Serializable]
    public class MenuItem : DataObject<MenuItem>
    {
        public virtual int ID
        {
            get { return GetValue<int>("ID"); }
            set
            {
                BeginEdit();
                SetValue("ID", value);
            }
        }

        [NonSerialized]
        Menu _menu;
        public virtual Menu Menu
        {
            get
            {
                return _menu;
            }
            set
            {
                BeginEdit();
                _menu = value;
            }
        }

        public virtual string Label
        {
            get { return GetValue<string>("Label"); }
            set
            {
                BeginEdit();
                SetValue("Label", value);
            }
        }

        public virtual string Title
        {
            get { return GetValue<string>("Title"); }
            set
            {
                BeginEdit();
                SetValue("Title", value);
            }
        }

        [NonSerialized]
        MenuItem _menuItem;
        public virtual MenuItem Parent
        {
            get
            {
                return _menuItem;
            }
            set
            {
                BeginEdit();
                _menuItem = value;
            }
        }

        [NonSerialized]
        private ISet<TaxonomyTag> _menuItemTaxonomyTags = new HashedSet<TaxonomyTag>();
        public virtual ISet<TaxonomyTag> MenuItemTaxonomyTags
        {
            get
            {
                return _menuItemTaxonomyTags;
            }
            set
            {
                BeginEdit();
                _menuItemTaxonomyTags = value;
            }
        }

        public virtual int SortOrder
        {
            get { return GetValue<int>("SortOrder"); }
            set
            {
                BeginEdit();
                SetValue("SortOrder", value);
            }
        }

        [XmlIgnore]
        public virtual IList<MenuItem> ChildItems
        {
            get
            {
                return Search(QrySearchChildMenuItems.Query(ID));
            }
        }

        /// <summary>
        /// Returns the most appropriate page bound to this menu item with a given context
        /// </summary>
        /// <param name="rootPageId">The page at the root of this menu items hierarchy</param>
        /// <returns>Best matched Content Page</returns>
        public virtual Contentpage GetPage(int rootPageId)
        {
            if (Menu.MenuType.IsContextual)
            {
                IQuery qry = QryFetchPageByMenuItem.Query(rootPageId, ID);
                return qry.UniqueResult<Contentpage>();
            }
            else
            {
                IQuery qry = QryFetchPageByMenuItem.Query(ID);
                return qry.UniqueResult<Contentpage>();
            }
        }

        public virtual DateTime RowStamp
        {
            get { return GetValue<DateTime>("RowStamp"); }
            set
            {
                SetValue("RowStamp", value);
            }
        }

       
    }
}
